﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using InHD.model;
using System.Windows.Input;
using InHD.dataaccess;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using InHD.commands;
using System.Windows;
using InHD.Properties;
using InHD.utilities.enums;

namespace InHD.viewmodels
{
    class SupplierViewModel: WorkspaceViewModel
    {
        #region Constructor
        public SupplierViewModel()
        {
            this._supplierRepository = new SupplierRepository();
            this._supplier = Supplier.CreateSupplier();
            LoadSupplierList();
            this.PageContent = new SuppliersListViewModel(this._supplierRowCollection);
            this.PageContent.RequestClose += this.HandleCloseCurrentPage;
        }
        #endregion

        #region Value Property
        private Supplier _supplier;
        readonly SupplierRepository _supplierRepository;
        private WorkspaceViewModel _pageContent;
        private List<Supplier> _supplierRowCollection = new List<Supplier>();
        private int _rowIndex = -1;
        #endregion

        #region Presentation Property

        public WorkspaceViewModel PageContent
        {
            get
            {
                return _pageContent;
            }
            set
            {
                _pageContent = value;
            }
        }

        #endregion

        #region Normal Method
        private void LoadSupplierList()
        {
            this._supplierRepository.LoadAllSuppliers();
            this._supplierRowCollection = this._supplierRepository.GetSuppliers();
        }

        private void HandleCloseCurrentPage(object source, EventArgs e)
        {
            this.PageContent.RequestClose -= this.HandleCloseCurrentPage;
            if (this.PageContent is SuppliersListViewModel)
            {
                string commandExcuted = ((SuppliersListViewModel)PageContent).commandExcute;
                if (string.IsNullOrEmpty(commandExcuted))
                {
                    OnRequestClose();
                    return;
                }

                if (commandExcuted.Equals("View"))
                {
                    this._supplier = ((SuppliersListViewModel)PageContent).SelectedSupplier.CreateShallowCopy();
                    this.PageContent = new SupplierFormViewModel(ActionModes.Edit, _supplier);
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
                else if (commandExcuted.Equals("Add"))
                {
                    this._supplier = Supplier.CreateSupplier();
                    this.PageContent = new SupplierFormViewModel(ActionModes.New, _supplier);
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
            }
            else if (this.PageContent is SupplierFormViewModel)
            {
                string commandExcuted = ((SupplierFormViewModel)PageContent).commandExcute;
                if (commandExcuted.Equals("Close"))
                {
                    this._supplier = null;
                    this.PageContent = new SuppliersListViewModel(_supplierRowCollection);
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
                else if (commandExcuted.Equals("Save"))
                {
                    if (((SupplierFormViewModel)PageContent).FormMode.Equals(ActionModes.New))
                    {
                        string result = this._supplierRepository.AddSupplier(((SupplierFormViewModel)PageContent).Supplier);
                        if (string.IsNullOrEmpty(result))
                        {
                            _supplierRowCollection = null;
                            _supplierRowCollection = _supplierRepository.GetSuppliers();
                            this.PageContent = new SuppliersListViewModel(_supplierRowCollection);

                        }
                        else if (result.Equals("DuplicateCode"))
                        {
                            MessageBox.Show(Resources.InHD_Error_DuplicateCode);
                            this.PageContent = new SupplierFormViewModel(ActionModes.New, ((SupplierFormViewModel)PageContent).Supplier);
                        }
                        else
                        {
                            MessageBox.Show(Resources.InHD_DB_Error);
                            this.PageContent = new SupplierFormViewModel(ActionModes.New, ((SupplierFormViewModel)PageContent).Supplier);
                        }
                    }
                    else
                    {
                        string result = this._supplierRepository.EditSupplier(((SupplierFormViewModel)PageContent).Supplier);
                        if (string.IsNullOrEmpty(result))
                        {
                            _supplierRowCollection = null;
                            _supplierRowCollection = _supplierRepository.GetSuppliers();
                            this.PageContent = new SuppliersListViewModel(_supplierRowCollection);
                        }
                        else if (result.Equals("DuplicateCode"))
                        {
                            MessageBox.Show(Resources.InHD_Error_DuplicateCode);
                            this.PageContent = new SupplierFormViewModel(ActionModes.Edit, ((SupplierFormViewModel)PageContent).Supplier);
                        }
                        else
                        {
                            MessageBox.Show(Resources.InHD_DB_Error);
                            this.PageContent = new SupplierFormViewModel(ActionModes.Edit, ((SupplierFormViewModel)PageContent).Supplier);
                        }
                    }

                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
                else if (commandExcuted.Equals("Delete"))
                {
                    if (!string.IsNullOrEmpty(_supplierRepository.DeleteSupplier(((SupplierFormViewModel)PageContent).Supplier.SupplierUNK)))
                    {
                        MessageBox.Show(Resources.InHD_DB_Error);
                    }
                    else
                    {
                        _supplierRowCollection = null;
                        _supplierRowCollection = _supplierRepository.GetSuppliers();
                    }

                    this.PageContent = new SuppliersListViewModel(_supplierRowCollection);
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
            }
        }
        #endregion

        public override string DisplayName
        {
            get
            {
                try
                {
                    return Resources.Title_Supplier;
                }
                catch (Exception ex)
                {
                    return string.Empty;
                }
            }

        }
    }
}
